package com.book.web;

import com.book.pojo.Book;
import com.book.pojo.Page;
import com.book.service.Impl.bookServiceImpl;
import com.book.service.bookService;
import com.book.utils.webUtils;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

public class bookServlet extends baseServlet {
    bookService bookService = new bookServiceImpl();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = webUtils.parseInt(request.getParameter("pageNo"), 0);
        pageNo+=1;
        Book book = new Book();
        webUtils.fillBean(book,request.getParameterMap());
        bookService.addBook(book);
        response.sendRedirect(request.getContextPath()+"/manager/bookManager?action=page&pageNo="+pageNo);
    }
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = new Book();
        webUtils.fillBean(book, request.getParameterMap());
        bookService.updateBook(book);
        response.sendRedirect(request.getContextPath()+"/manager/bookManager?action=page&pageNo="+request.getParameter("pageNo"));
    }
    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1 获取请求的参数图书编号
        int id = webUtils.parseInt(request.getParameter("id"),0);
        //2 调用bookService.queryBookById查询图书
        Book book = bookService.queryBookById(id);
        //3 保存到图书到Request域中
        request.setAttribute("book", book) ;
        //4 请求转发到。pages/manager/book_edit.jsp页面
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
    }
    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        bookService.delBookById(webUtils.parseInt(request.getParameter("id"),0));
        response.sendRedirect(request.getContextPath()+"/manager/bookManager?action=page&pageNo="+request.getParameter("pageNo"));
    }
    protected void queryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Book> books = bookService.queryBooks();
        request.setAttribute("books", books);
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }

    /**
     * 处理分页功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = webUtils.parseInt(request.getParameter("pageNo"),1);
        int pageSize = webUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Book> page = bookService.page(pageNo, pageSize);
        page.setUrl("manager/bookManager?action=page");
        //保存到request域中
        request.setAttribute("page", page);
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }
}
